package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Driver;
import com.blb.entity.User;
import com.blb.entity.Vehicle;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.IDriverService;
import com.blb.service.ILogisticsService;
import com.blb.service.IVehicleService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-16
 */
@Controller
@RequestMapping("/vehicle")
public class VehicleController {

    @Resource
    private IVehicleService iVehicleService;
    @Resource
    private ILogisticsService iLogisticsService;
    @Resource
    private IDriverService iDriverService;

    @RequestMapping("{page}")
    public String wel(@PathVariable String page){
        return "logistics/"+ page;
    }



    /*显示所有商品*/
    @ResponseBody
    @RequestMapping("findAll")
    public BlbPage findAllProduct(@RequestParam(required = false, value ="vehiclename") String vehiclename,@RequestParam(required = false, value ="number") String number, Integer start, Integer length){
        System.out.println(vehiclename);

        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();

        List<Vehicle> findall = iVehicleService.findAll(vehiclename,companyId, number, start, length);
        /*查询分页数据*/

        System.out.println(findall);

        int count = iVehicleService.getAllCount(vehiclename,companyId, number);

        return new DataTablePage(count,count,findall);
    }

    @ResponseBody
    @RequestMapping("deletevehicle")
    public ResponseHelp deleteVehicle(String id){
        iVehicleService.removeById(id);
        ResponseHelp responseHelp =new ResponseHelp();
        return responseHelp;
    }

    @ResponseBody
    @RequestMapping("findremarks")
    public ResponseHelp findRemarks(String id){

        String reamrksById = iVehicleService.findReamrksById(id);

        ResponseHelp responseHelp =new ResponseHelp();
        responseHelp.setData(reamrksById);
        return responseHelp;
    }

    @ResponseBody
    @RequestMapping("updatevehicle")
    public ResponseHelp  updateVehicle(Vehicle vehicle){
        System.out.println(vehicle);
        System.out.println("修改进来了");
        System.out.println(vehicle);
        User user = ShiroUtils.getUser();
        String userId = user.getId();
        vehicle.setUpdateId(userId);
        vehicle.setUpdateTime(new Date());
        iVehicleService.updateVehicle(vehicle);
        ResponseHelp responseHelp =new ResponseHelp();
        return responseHelp;
    }

    @ResponseBody
    @RequestMapping("addvehicle")
    public ResponseHelp addVehicle(Vehicle vehicle){
        User user = ShiroUtils.getUser();
        String userId = user.getId();
        vehicle.setCreateId(userId);
        vehicle.setVehicleId(UUIDUtils.getId());
        vehicle.setCreateTime(new Date());
        vehicle.setStatus("无任务");
        iVehicleService.save(vehicle);
        ResponseHelp responseHelp =new ResponseHelp();
        return responseHelp;
    }

    @ResponseBody
    @RequestMapping("findallxx")
    public List<Vehicle> findAllxx(String id){
        List<Vehicle> list = iVehicleService.list(new QueryWrapper<Vehicle>().eq("logistics_id", id));
        return list;
    }
}

